package com.bdq.user.infrastructure.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.bdq.user.infrastructure.model.dto.ImImportAccountDTO;
import com.bdq.user.infrastructure.util.tencent.TLSSigAPIv2;
import lombok.extern.slf4j.Slf4j;

/**
 * @author malei
 * @date 2024-07-23 18:06
 */
@Slf4j
public class TencentImUtil {

    /**
     * SDK APP ID
     */
    public static final Long SDK_APP_ID = 1600034733L;

    /**
     * SDK APP SECRET
     */
    public static final String SDK_APP_SECRET = "2085b751561d201080f61080e5c8ef046c2c61a7858d42868abb8958d370e0ba";

    /**
     * 管理员账户 USER ID
     */
    public static final String ADMIN_ACCOUNT_USER_ID = "administrator";

    /**
     * 签名过期时间
     */
    public static final Long USER_SIG_EXPIRE = 1000 * 60L;

    /**
     * 单个账户导入
     *
     * @param userId
     * @param nick
     * @param faceUrl
     */
    public static void singleAccountImport(Long userId, String nick, String faceUrl) {
        TLSSigAPIv2 api = new TLSSigAPIv2(SDK_APP_ID, SDK_APP_SECRET);
        String userSig = api.genUserSig(ADMIN_ACCOUNT_USER_ID, USER_SIG_EXPIRE);

        String url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=%s&identifier=%s&usersig=%s&random=99999999&contenttype=json";
        url = String.format(url, SDK_APP_ID, ADMIN_ACCOUNT_USER_ID, userSig);

        ImImportAccountDTO imImportAccountDTO = ImImportAccountDTO.builder().userId(String.valueOf(userId)).nick(nick).faceUrl(faceUrl).build();
        String result = HttpUtil.post(url, JSON.toJSONString(imImportAccountDTO));
        log.info("[账号导入结果]result:{}", result);
    }
}
